package com.example.administrator.zhjt.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.administrator.zhjt.bean.Environment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/16 0016.
 */

public class EnvironmentDB extends SQLiteOpenHelper {

    public static final String ENVIRONMENT_TABLE = "environment_table";
    public static final int KEY_VERSION = 1;
    public static final String KEY_TEMPERATURE = "temperature";
    public static final String KEY_HUMIDITY = "humidity";
    public static final String KEY_LIGHTINTENSITY = "LightIntensity";
    public static final String KEY_CO2 = "co2";
    public static final String KEY_PM = "pm";
    public static final String KEY_BALANCE = "balance";
    public static final String KEY_CREATEAT = "createAt";
    public static final String[] KEY_SET = {
            KEY_TEMPERATURE,
            KEY_HUMIDITY,
            KEY_LIGHTINTENSITY,
            KEY_CO2,
            KEY_PM,
            KEY_BALANCE
    };

    public static final String CREATE_TABLE = "create table " + ENVIRONMENT_TABLE
            + "(_id integer primary key autoincrement,"
            + KEY_TEMPERATURE + " Long,"
            + KEY_HUMIDITY + " Long,"
            + KEY_LIGHTINTENSITY + " Long,"
            + KEY_CO2 + " Long,"
            + KEY_PM + " Long,"
            + KEY_BALANCE + " Long,"
            + KEY_CREATEAT + " Long);";

    public EnvironmentDB(Context context) {
        super(context, ENVIRONMENT_TABLE, null, KEY_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public List<Environment> list(String order) {
        List<Environment> list = new ArrayList<>();
        SQLiteDatabase database = getReadableDatabase();
        Cursor cursor = database.rawQuery("select * from" + ENVIRONMENT_TABLE + "order by" + KEY_CREATEAT + " " + order, null);
        while (cursor.moveToNext()) {
            Environment environment = new Environment();
            list.add(environment.fromCursor(cursor));
        }
        return list;
    }
}
